package com.synjones.beijing
{
	import mx.collections.ArrayCollection;

	public final class MenuLocator
	{
		static private var STATE_SCHRODINGER:String = "schrodinger";
		static private var STATE_CHECKED:String = "checked";
		static private var STATE_UNCHECKED:String = "unchecked";
		
		private static var menu:XML;
		private static var resourceList:String="";
		private static var oldArray:ArrayCollection;
		public static function GetMenuXML(resource:ArrayCollection,reList:String):XML{
			if (oldArray != resource || resourceList!=reList){
				resourceList = reList;
	            var xl:XML=new XML("<menues></menues>");
	            var i:int=0;
	            var state:String = "";
		        for(i=0;i<resource.length;i++){
		        	var item:Object = resource.getItemAt(i);
		        	if(item.PARENTID=="000"){
		        		var xm:XML
		        		if(reList.indexOf(item.CODE+",",0)>=0){
		        			if(state == STATE_UNCHECKED){
		        				state = STATE_SCHRODINGER;
		        			}
		        			else if(state == ""){
		        				state = STATE_CHECKED;
		        			}
		        			xm = new XML("<node label=\""+item.NAME+"\" data=\""+item.URL+"\" code=\""+item.CODE+"\" state=\""+STATE_CHECKED+"\" selected=\"true\"></node>");
		        		}else{
		        			if(state == STATE_CHECKED){
		        				state = STATE_SCHRODINGER;
		        			}
		        			else if(state == ""){
		        				state = STATE_UNCHECKED;
		        			}
		        			xm = new XML("<node label=\""+item.NAME+"\" data=\""+item.URL+"\" code=\""+item.CODE+"\" state=\""+STATE_UNCHECKED+"\" selected=\"false\"></node>");
		        		}
		        		GetChildMenueXML(resource,item.CODE,xm,reList);
		        		xl.appendChild(xm);
		        	}
		        }
		        if(state != ""){
		        	xl.@state=state;
		        	xl.@isBranch="true";
		        }else{
		        	xl.@isBranch = "false";
		        } 
		        menu = xl;
	        }
	        return menu;
		}
		
		private static function GetChildMenueXML(resource:ArrayCollection,parentID:String,xmp:XML,reList:String):void
		{
			var i:int=0;
			var state:String = "";
	        for(i=0;i<resource.length;i++){
	        	var item:Object = resource.getItemAt(i);
	        	if(item.PARENTID==parentID){
	        		var xm:XML
	        		if(reList.indexOf(item.CODE+",",0)>=0){
	        			if(state == STATE_UNCHECKED){
	        				state = STATE_SCHRODINGER;
	        			}else if(state == ""){
	        				state = STATE_CHECKED;
	        			}
	        			xm = new XML("<node label=\""+item.NAME+"\" data=\""+item.NAME+"\" code=\""+item.CODE+"\" state=\""+STATE_CHECKED+"\" selected=\"true\"></node>");
	        		}else{
	        			if(state == STATE_CHECKED){
	        				state = STATE_SCHRODINGER;
	        			}else if(state == ""){
	        				state = STATE_UNCHECKED;
	        			}
	        			xm = new XML("<node label=\""+item.NAME+"\" data=\""+item.URL+"\" code=\""+item.CODE+"\" state=\""+STATE_UNCHECKED+"\" selected=\"false\"></node>");
	        		}
	        		GetChildMenueXML(resource,item.CODE,xm,reList);
		        	xmp.appendChild(xm);
	        	}
	        }
	        if(state != ""){
	        	xmp.@state=state;
	        	xmp.@isBranch="true";
	        }else{
	        	xmp.@isBranch = "false";
	        } 
		}
	}
}